package org.cytoscape.myapp.my_cyaction_app.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Vector;

import org.cytoscape.group.CyGroup;
import org.cytoscape.group.CyGroupFactory;
import org.cytoscape.group.CyGroupManager;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;


public class CyRangeSorter extends CySorter {

	private Integer valInf;
	private Integer valSup;
	
	public CyRangeSorter(CyGroupFactory FactoryGroup, CyGroupManager ManagerGroup, String NameAttribute, Integer valInf, Integer valSup){
		super(FactoryGroup,ManagerGroup,NameAttribute);
		this.valInf = valInf;
		this.valSup = valSup;
	}
	
	private boolean pertenece(Long IdNode, CyRow Row){
		Object value =Row.getRaw(this.GetNameAttribute());
		if ((valInf.compareTo((Integer) value) <= 0) && (valSup.compareTo((Integer) value) > 0))
			return true;
		return false;
	}
	
	@Override
	public CyGroup GetCyGroupClasificated(CyNetwork network) {
		CyGroup grupo = FactoryGroup.createGroup(network, true);
		CyTable table = network.getDefaultNetworkTable();
		final Collection<CyRow> AllRows = table.getAllRows();
		List<CyNode> nodes = new ArrayList<CyNode>();
		final String primaryKeyColname = table.getPrimaryKey().getName();
        for (final CyRow row : AllRows)
        {
            final Long nodeId = row.get(primaryKeyColname, Long.class);
            if (nodeId == null)
                continue;
            final CyNode node = network.getNode(nodeId);
            if (node == null)
                continue;
            else
            {
            	if (pertenece(nodeId, row))
            		nodes.add(node);
            }
        }
		grupo.addNodes(nodes);
		return grupo;
	}

}
